const app = getApp(), fetchApi = require("../../utils/httpPost.js"), ad = require("../../template/ad/adTemplate.js"),
commonJs = require("../../component/common.js");
import Toast from '../../component/vant/toast/toast';
const CryptoJS = require("../../utils/aesUtil.js")
Page({
  data: {
    imgurl: app.getImgUrl(),
    userInfo: {},
    hasUserInfo: false,
    orderItemArr: [],
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    isLogin:0,
    phone_num:'',
    isBandPhone:0,
    user_id:0,
    kfPhone:'',
    nick_name:'',
    face_url:'',
  },
  //事件处理函数
  bindViewTap: function() {
    // wx.navigateTo({ url: '../logs/logs' })
  },
  onLoad: function () {
    // this.doLogin()    
  },
  goLogin:function(){
    wx.navigateTo({
      url: '/pages/login/index',
    })
  },
  doLogin: function(){
    var that = this
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log("code", res.code)
        fetchApi.httpPost('/wxUser/wxLogin', { code: res.code }, function (res) {
          console.log("登录返回数据", res)
          if(res.data.code == 0){
            // that.onHttpGetUserInfo()
          }
          that.setData({ isLoading: false, loading: true })
        }, function (res) { 
          that.setData({ isLoading: false, loading: true })
        })
      },fail(res){
        that.setData({ isLoading: false, loading: true })
      }
    })
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/wxUser/getUserInfoData', {
    }, function (res) {
      console.log("getUserInfoData 数据", res)
      if (res.data.code == 0) {
        that.data.user_id =  res.data.data.user_id;
        that.data.kfPhone = res.data.data.kfPhone;
        that.setData({ 
          orderItemArr: res.data.data.orderItemArr,
          phone_num: res.data.data.phone_num!=null&&res.data.data.phone_num.length > 0 ? res.data.data.phone_num :'请绑定手机号',
          isBandPhone:res.data.data.phone_num!=null&&res.data.data.phone_num.length > 0?1:0,
          nick_name:res.data.data.nick_name, 
          face_url :res.data.data.face_url,
        })
        if(res.data.data.phone_num!=null && res.data.data.phone_num.length > 0){
          app.setStorageSync("phone_num", res.data.data.phone_num);
        }
      }
    },function(){})
  },
  onShow: function () {
    var that = this
    var isLogin = app.getStorageSync("isLogin");
    app.globalData.userInfo = app.getStorageSync("userInfo");
    console.log('isLogin', isLogin)
    console.log('app.globalData.userInfo', app.globalData.userInfo)
    if (isLogin == 1) {
      that.setData({
        isLogin: isLogin,
        userInfo: app.globalData.userInfo
      })
      that.onHttpInit()
    }
  },
  shop_order:function(e){
    var url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: url,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  pushToAd: function (e) {
    console.log('pushToAd e', e)
    ad.pushToAd(e, 'personal')
  },
  callPlatform(e){
    var that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.kfPhone,
    })
  },
  // 客服电话 4000012800
  kefu:function(){
    wx.navigateTo({
      url: '/pages/personal/customer/index',
    })
  },
  relation(){
    wx.navigateTo({
      url: '/pages/relation/index',
    })
  },
  account(){
    wx.navigateTo({
      url: '/pages/account/index',
    })
  }
})
